package com.topcheer.gateway;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.topcheer.gateway.common.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xutao
 * @Package com.topcheer.configserver
 * @Description: ${todo}
 * @date 2020/1/2 15:59
 */
@RestController
public class InstanceController {

    @Autowired
    EurekaClient eurekaClient;

    @GetMapping("/instanceInfo")
    public InstanceInfo getInstanceInfo(){
        InstanceInfo info = eurekaClient.getApplicationInfoManager().getInfo();
        return info;
    }


    @PutMapping("/instance/shutdown")
    public void shutdown(){
//        eurekaClient.shutdown();
        eurekaClient.getApplicationInfoManager().setInstanceStatus(InstanceInfo.InstanceStatus.DOWN);
    }

    @RequestMapping(value = "/hystrix/fallback")
    public ResultVo fallBackController() {
        return ResultVo.builder().code(HttpStatus.GATEWAY_TIMEOUT.value()).msg(HttpStatus.GATEWAY_TIMEOUT.getReasonPhrase()).build();
    }
}
